package vikaa.versonservice;


import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import vikaa.tools.HttpUtil;
import vikaa.wowo.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.IBinder;

public class VersonService extends Service {
	public static final int SUCCESS = 1;
	private static final String TAG = "VersonService";
	private int                  	newVerCode;
	private String                  newVerName;
	private String                 	appName;
	private String                  appUrl;
	private static int              VerCode;
	private static String           VerName;
	private NotificationManager		mNotifMan;
	// Notification title
	public static String			NOTIF_TITLE = "窝窝"; 	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	@Override
	public void onCreate() {
		VerCode = getVerCode(VersonService.this); 
		VerName = getVerName(VersonService.this);
	}

	@Override
	public void onDestroy() {
	}
	
	@Override
	public void onStart(Intent intent, int startid) {
		new Thread(new Runnable() {
			
			public void run() {
				// TODO Auto-generated method stub
				if(getServerVer()){
					if(newVerCode > VerCode){
						Notification();
			 			stopSelf();
					}
				}
				else
					stopSelf();
			}
		}).start();
	}
	private void Notification() {
		mNotifMan = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		Notification n = new Notification(R.drawable.icon,NOTIF_TITLE, System.currentTimeMillis());
				
		n.flags |= Notification.FLAG_SHOW_LIGHTS;
      	n.flags |= Notification.FLAG_AUTO_CANCEL;
      	
        n.defaults = Notification.DEFAULT_ALL;
        //n.flags|=Notification.FLAG_ONGOING_EVENT;
	    Intent intent = new Intent(this, VersonUpdate.class);
			// Simply open the parent activity
		PendingIntent pi = PendingIntent.getActivity(this, 0,
		intent, 0);
			// Change the name of the notification here
		n.setLatestEventInfo(this, NOTIF_TITLE, getText(R.string.found_verson), pi);
		mNotifMan.notify(0, n);
	}
	public static int getVerCode(Context context) {  
        int verCode = -1;  
        try {  
            verCode = context.getPackageManager().getPackageInfo(  
                    "vikaa.wowo", 0).versionCode;  
        } catch (NameNotFoundException e) {  
        }  
        return verCode;  
    }  
    //get verson name from app
    public static String getVerName(Context context) {  
        String verName = "";  
        try {  
            verName = context.getPackageManager().getPackageInfo(  
                    "vikaa.wowo", 0).versionName;  
        } catch (NameNotFoundException e) {  
        }  
        return verName;     
    }
	 // get verson code from server
    private boolean getServerVer () {  
    	Map<String, String> map = new HashMap<String, String>();
		map.put("os_verson", android.os.Build.VERSION.RELEASE);
        try {  
        	String url = HttpUtil.BASE_URL + "corel/apkupdate";
            JSONObject jsonObj = new JSONObject(HttpUtil.postRequest(url, map));
            String result = jsonObj.getString("result");
            if (result.equals("1")) {  
                try {  
                    newVerCode = Integer.parseInt(jsonObj.getJSONObject("info").getString("verCode"));  
                    newVerName = jsonObj.getJSONObject("info").getString("verName");  
                    appName = jsonObj.getJSONObject("info").getString("appName");  
                    appUrl = jsonObj.getJSONObject("info").getString("appUrl");  
                } catch (Exception e) {  
                    newVerCode = -1;  
                    newVerName = "";  
                    return false;  
                }  
            } 
            else
            	return false;
        } catch (Exception e) {  
            return false;  
        }  
        return true;  
    } 
}
